<template>
  <div class="mod-user">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.searchKey" placeholder="订单号" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="100">
      </el-table-column>
      <el-table-column
        prop="orderId"
        header-align="center"
        align="center"
        label="订单号">
      </el-table-column>
      <el-table-column
        prop="buyerName"
        header-align="center"
        align="center"
        label="买家姓名">
      </el-table-column>
      <el-table-column
        prop="buyerPhone"
        header-align="center"
        align="center"
        label="买家手机">
      </el-table-column>
      <el-table-column
        prop="buyerAddress"
        header-align="center"
        align="center"
        label="买家地址">
      </el-table-column>
      <el-table-column
        prop="orderAmount"
        header-align="center"
        align="center"
        label="订单总价">
      </el-table-column>
      <el-table-column
        prop="orderStatus"
        header-align="center"
        align="center"
        label="订单状态">
        <template slot-scope="scope">
          {{statusParse(scope.row.orderStatus, scope.row.payStatus)}}
        </template>
      </el-table-column>
      <!--<el-table-column-->
        <!--prop="payStatus"-->
        <!--header-align="center"-->
        <!--align="center"-->
        <!--label="支付">-->
        <!--<template slot-scope="scope">-->
          <!--{{payParse(scope.row.payStatus)}}-->
        <!--</template>-->
      <!--</el-table-column>-->
      <el-table-column
        prop="createDate"
        header-align="center"
        align="center"
        label="创建时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="addOrUpdateHandle(scope.row.orderId)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './order-add-or-update'
  import orderStatusEnum from '@/enums/orderStatusEnum'
  import payStatusEnum from '@/enums/payStatusEnum'

  export default {
    data () {
      return {
        dataForm: {
          searchKey: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        productCategoryList: [],
        orderStatusEnum: orderStatusEnum,
        payStatusEnum: payStatusEnum
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true

        this.$http({
          url: this.$http.adornUrl('/sys/order/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'searchKey': this.dataForm.searchKey
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            console.log('data.page.list ===', data.page.list)
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
        // this.$http({
        //   url: this.$http.adornUrl('/sys/productCategory/list'),
        //   method: 'get'
        // }).then(({data}) => {
        //   if (data && data.code === 0) {
        //     this.productCategoryList = data.productCategoryList
        //   }
        //   this.$http({
        //     url: this.$http.adornUrl('/sys/goods/list'),
        //     method: 'get',
        //     params: this.$http.adornParams({
        //       'page': this.pageIndex,
        //       'limit': this.pageSize,
        //       'searchKey': this.dataForm.searchKey
        //     })
        //   }).then(({data}) => {
        //     if (data && data.code === 0) {
        //       console.log('data.page.list ===', data.page.list)
        //       this.dataList = data.page.list
        //       this.totalPage = data.page.totalCount
        //     } else {
        //       this.dataList = []
        //       this.totalPage = 0
        //     }
        //     this.dataListLoading = false
        //   })
        // })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      statusParse (orderStatus, payStatus) {
        // for (let key in this.orderStatusEnum) {
        //  if (key == value) {
        //     return this.orderStatusEnum[key].message
        //   }
        // }
        if (orderStatus === 0 && payStatus === 0) {
          return '等待付款'
        }
        if (orderStatus === 0 && payStatus === 1) {
          return '等待发货'
        }
        if (orderStatus === 1 && payStatus === 1) {
          return '等待收货'
        }
        if (orderStatus === 2) {
          return '已完成'
        }
        if (orderStatus === 3) {
          return '已取消'
        }
        if (orderStatus === 4) {
          return '等待拼团'
        }
        if (orderStatus === 5) {
          return '拼团完成'
        }

        return ''
      },
      payParse (value) {
        for (let key in this.payStatusEnum) {
          if (key === value) {
            return this.payStatusEnum[key].message
          }
        }
        return ''
      }
    }
  }
</script>
